package com.czxy.servlet;

import cn.itcast.servlet.BaseServlet;
import com.czxy.domain.User;
import com.czxy.service.UserService;

import javax.servlet.annotation.WebServlet;
import java.io.IOException;

@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {

    UserService service = new UserService();


    public String register(){

        User user = toBean(User.class);

        UserService service = new UserService();

        service.register(user);

        return "/jsp/login.jsp";



    }





    public  void  checkUsername() throws IOException {

        //接收 username
        String username = getRequest().getParameter("username");


        UserService service = new UserService();


        User u = service.findUserByUsername(username);

        if (u == null){
            //用户名可用
//            {"message":"用户名可用","flag":true}

            getResponse().getWriter().write("{\"message\":\"用户名可用\",\"flag\":true}");
        }else{

            getResponse().getWriter().write("{\"message\":\"用户名不可用\",\"flag\":false}");
        }


    }
}
